from ..base import PermissionRuleHandler, register_handler

@register_handler("field_match")  
class FieldMatchHandler(PermissionRuleHandler):
    def get_required_config_keys(self):
        return ["user_field", "target_field"]

    def validate_config(self, config):
        return all(k in config for k in self.get_required_config_keys())

    def generate_sql(self, table_alias: str, config: dict, user: dict) -> str:    
        user_field = config["user_field"]
        target_field = config["target_field"]
        user_val = user.get(user_field)
    
        if user_val is None:
            return "1=0"
        
        # 如果是字符串值，需要加引号
        if isinstance(user_val, str):
            result = f"{table_alias}.{target_field} = '{user_val}'"
            return result
        else:
            result = f"{table_alias}.{target_field} = {user_val}"
            return result